package com.spring.mvc.util;

import static com.spring.mvc.util.ErrCode.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.spring.mvc.pojo.CallResult;
import com.spring.mvc.pojo.User;

/**
 * 参数检测
 * @author liujian
 *
 */
public class ParamCheck {
	
	private static final Logger logger=LoggerFactory.getLogger(ParamCheck.class);
	
	public static final String USER_NAME_REGEX="^[a-zA-Z0-9]{3,15}$";
	
	public static final String USER_PWD_REGEX="^.{5,15}$";
	
	public static final String USER_EMAIL_REGEX="^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+";

	/**
	 * 检查用户注册
	 * @param user
	 * @param result
	 * @return
	 */
	public static void checkUserRegister(User user,CallResult result){
		result.setErrCode(SUCCESS);
		try {
			if(checkUserName(user.getUserName(),result)){
				if(checkUserPwd(user.getUserPwd(), result)){
					if(checkPwdInconsistent(user.getUserPwd(), user.getUserPwd2(), result)){
						user.setUserPwd(MD5Util.compute(user.getUserPwd()));
						if(checkEmail(user.getEmail(), result)){
							if(StringUtil.isEmpty(user.getAddress())){
								user.setAddress("");
							}else if(StringUtil.isEmpty(user.getPhone())){
								user.setPhone("");
							}else if(StringUtil.isEmpty(user.getMobile())){
								user.setMobile("");
							}
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("user register param check error!",e);
		}
	}
	
	/**
	 * 用户登录参数检查
	 * @param user
	 * @param result
	 */
	public static boolean checkUserLogin(User user,CallResult result){
		result.setErrCode(SUCCESS);
		try {
			if(checkUserName(user.getUserName(), result)){
				if(checkUserPwd(user.getUserPwd(), result)){
					user.setUserPwd(MD5Util.compute(user.getUserPwd()));
					return true;
				}
			}
		} catch (Exception e) {
			logger.error("user login param check error!",e);
		}
		return false;
	}
	
	/**
	 * 检查用户名的合法性
	 * @param userName
	 * @param result
	 */
	public static boolean checkUserName(String userName,CallResult result){
		if(StringUtil.isEmpty(userName)){
			result.setErrInfo(ERROR, Prompt.get("user.name.null"));
		}else if(!userName.matches(USER_NAME_REGEX)){
			result.setErrInfo(ERROR, Prompt.get("user.name.format.error"));
		}
		return result.getErrCode()==SUCCESS;
	}
	
	/**
	 * 检查用户密码的合法性
	 * @param userPwd
	 * @param result
	 */
	public static boolean checkUserPwd(String userPwd,CallResult result){
		if(StringUtil.isEmpty(userPwd)){
			result.setErrInfo(ERROR, Prompt.get("user.password.null"));
		}else if(!userPwd.matches(USER_PWD_REGEX)){
			result.setErrInfo(ERROR, Prompt.get("user.password.format.error"));
		}
		return result.getErrCode()==SUCCESS;
	}
	
	/**
	 * 检查两次输入密码是否一致
	 * @param userPwd
	 * @param userPwd2
	 * @param result
	 * @return
	 */
	public static boolean checkPwdInconsistent(String userPwd,String userPwd2,CallResult result){
		if(!userPwd.equals(userPwd2)){
			result.setErrInfo(ERROR, Prompt.get("user.password.inconsistent"));
		}
		return result.getErrCode()==SUCCESS;
	}
	
	/**
	 * 检查邮箱的合法性
	 * @param email
	 * @param result
	 * @return
	 */
	public static boolean checkEmail(String email,CallResult result){
		if(StringUtil.isEmpty(email)){
			result.setErrInfo(ERROR, Prompt.get("user.email.null"));
		}else if(!email.matches(USER_EMAIL_REGEX)){
			result.setErrInfo(ERROR, Prompt.get("user.email.format.error"));
		}
		return result.getErrCode()==SUCCESS;
	}
	
	public static void main(String[] args) {
		CallResult result=new CallResult();
		User user=new User();
		user.setUserName("aain");
		user.setUserPwd("admins");
		user.setUserPwd2("admins");
		user.setEmail("asdfasd@qq.com");
		checkUserRegister(user, result);
		System.out.println(result.getErrCode()+";"+result.getErrDesc());
	}
}
